<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 16-12-16
 * Time: 下午5:41
 */

namespace common\services\common;


use common\repositories\common\MyscrmContractApplicationRepository;
use common\repositories\myhtsq\ApplicationInfoRepository;
use common\services\BaseService;

class MyscrmContractApplicationService extends BaseService
{

    private $repository;

    public function __construct(MyscrmContractApplicationRepository $repository, $config = [])
    {
        $this->repository = $repository;

        parent::__construct($config);
    }

    public function getContractApplicationByContractId($contractId)
    {
        return $this->repository->getContractApplicationByContractId($contractId);
    }

    /**
     * 出库中的产品转为合同管理中的产品
     * @param $outStockProduct
     * @return array|bool
     */
    public function parseContractApplication($outStockProduct)
    {
        $result = [];
        if (empty($outStockProduct)) {
            return false;
        }
        //获取产品单价计算产品价格 
        $appInfo = ApplicationInfoRepository::getApplicationInfoById($outStockProduct["application_id"]);
        if (empty($appInfo)) {
            return false;
        }
        $result["application_id"] = $outStockProduct["application_id"];
        $result["rent_period"] = $outStockProduct["rent_period"];
        $result["authorization_numeric"] = $outStockProduct["apply_num"];
        $result["contract_type"] = $outStockProduct["contract_type"];
        $result["authorization_type"] = $appInfo["authorization_type"];
        $result["price"] = $appInfo["price"];
        $result["unit"] = $appInfo["unit"];
        $result["amount"] = 0;
        if ($appInfo["authorization_type"] == 0) {
            //计算amount
            $result["amount"] = round(($result["rent_period"] / 12) * $result["price"], 2);
        }
        return $result;
    }

    /**
     * @sine 2017-7-25
     * @auth zhoud01@mingyuanyun.com
     * 获取产品信息
     */
    public static function getApplicationInfoByApplicationId($application_id)
    {
        //修复坏味道
        return ApplicationInfoRepository::getApplicationInfoByApplicationId($application_id);
    }

    /**
     * @sine 2017-8-19
     * @auth zhoud01@mingyuanyun.com
     * 获取产品信息通过uniquename
     */
    public static function getApplicationInfoByAuthorizeUniqueName($authorize_unique_name,$IsQdAdvanced=0)
    {
        //修复坏味道
        return ApplicationInfoRepository::getApplicationInfoByAuthorizeUniqueName($authorize_unique_name,$IsQdAdvanced);
    }


}